<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Class Controller_Member_Register
 * 会员注册
 */
class Controller_Member_Register extends Stourweb_Controller
{

    //注册首页
    public function action_index()
    {
        if(St_Functions::is_normal_app_install('mobiledistribution'))
        {
            Model_Fenxiao::save_fxcode();
        }
        $this->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $this->cmsurl;
        if (!stripos($this->referer, '/message/') && !stripos($this->referer, '/member/'))
        {
            Cookie::set('referer', $this->referer);
        }
        $this->assign('referer', Cookie::get('referer', $this->cmsurl));
        $sms = DB::select()->from('sms_msg')->where('msgtype="reg_msgcode"')->execute()->current();
        $this->assign('isopen', $sms['isopen']);

        $emailcode_sms = DB::select()->from('email_msg')->where('msgtype="reg_msgcode"')->execute()->current();
        $this->assign('is_emailcode_open',$emailcode_sms['isopen']);

        $agreement=Model_Sysconfig::get_configs(0,'cfg_member_agreement_open',1);
        $this->assign('is_agreement',$agreement);
        $token = md5(time());
        Common::session('crsf_code', $token);
        $this->assign('frmcode', $token);
        $this->assign('url', $this->cmsurl . 'member');
        $this->display('member/register','member_reg');
    }

    //注册执行
    public function action_ajax_reg()
    {
        $user = Common::remove_xss(Arr::get($_POST, 'user'));
        $pwd = common::pwd_decrypt(Common::remove_xss(Arr::get($_POST, 'pwd')),'cms_st');
        $frmcode = Common::remove_xss(Arr::get($_POST, 'frmcode'));
        $code = Common::remove_xss(Arr::get($_POST, 'code'));
        $msgcode = Common::remove_xss(Arr::get($_POST, 'msg'));
        $company_name = St_String::filter_mark(strip_tags($_POST['company_name']));

        if ($frmcode != Common::session('crsf_code'))
        {
            Common::session('captcha_response', null);
            $message = array('msg' => __("error_frmcode"), 'status' => 0);
            exit(json_encode($message));
        }

        //$captcha_code = Common::session('securimage_code_value');
        //$captcha = $captcha_code['default'];
        //验证码验证
        if (!Captcha::valid($code) || empty($code))
        {
            Common::session('captcha_response', null);
            $message = array('msg' => __("account_code_error"), 'status' => 0);
            exit(json_encode($message));
        }

        if (empty($msgcode))
        {
            Common::session('captcha_response', null);
            $message = array('msg' =>'动态验证码为空', 'status' => 0);
            exit(json_encode($message));
        }

        if (Common::session($user.'_msg_code') != $msgcode)
        {
            Common::session('captcha_response', null);
            $message = array('msg' => __("account_sms_code_error"), 'status' => 0);
            exit(json_encode($message));
        }

        //密码复杂度，至少要6位，至少一个大写字母一个小写字母至少一个数字
        if(!preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{6,}$/', $pwd)){
            Common::session('captcha_response', null);
            $message = array('msg' =>'密码至少要6位、至少包含1个大写字母1个小写字母1个数字', 'status' => 0);
            exit(json_encode($message));
        }

        $userType = isset($_POST['is_email']) ? 'email' : 'phone';
        $validataion = Validation::factory($this->request->post());
        $validataion->rule('pwd', 'not_empty');
        $validataion->rule('pwd', 'min_length', array(':value', '6'));
        $validataion->rule('user', 'not_empty');
        $validataion->rule('user', $userType);
        if (!$validataion->check())
        {
            $error = $validataion->errors();
            $keys = array_keys($validataion->errors());
            $message = array('msg' => __("error_{$keys[0]}_{$error[$keys[0]][0]}"), 'status' => 0);
            exit(json_encode($message));
        }
        //用户不存在则添加
        $member = Model_Member::member_find($user);
        if (!empty($member))
        {
            $message = array('msg' => __("error_user_exists"), 'status' => 0);
            exit(json_encode($message));
        }
        $isPhone = false;
        $regtype = 0;
        if ($userType == 'phone')
        {
            $isPhone = true;
            $userType = 'mobile';
            $nickname=substr($user,0,5).'***';
        }
        else
        {
            $regtype = 1;
            $nickname = $user;
        }

        $addtime = time();
        list($insertId, $rows) = DB::insert('member', array('nickname', $userType, 'jointime', 'logintime', 'pwd', 'regtype','company_name'))->values(array($nickname, $user, $addtime, $addtime, md5($pwd), $regtype,$company_name))->execute();
        Model_Member::add_reg_from($insertId,2);
        if ($rows > 0)
        {
            Common::session('crsf_code', null);
            Common::session('captcha_response', null);
            Common::session('msg_code', null);

            //发送注册成功信息
            if ($isPhone)
            {
                St_SMSService::send_member_msg($user,NoticeCommon::MEMBER_REG_MSGTAG,$user,$pwd,"");
            } else
            {
                St_EmailService::send_member_email($user,NoticeCommon::MEMBER_REG_MSGTAG,$pwd,"");
            }


            //注册送积分
            $jifen = Model_Jifen::reward_jifen('sys_member_register',$insertId);
            if(!empty($jifen))
            {
                St_Product::add_jifen_log($insertId,"注册赠送积分{$jifen}",$jifen,2);
            }

            //登录状态
            Model_Member::write_session(Model_Member::get_member_byid($insertId));
            //是否是红包领取跳转
            $envelope_order_id = Common::session('envelope_order_id');
            if($envelope_order_id)
            {
                Common::session('envelope_new_member',1);

            }
            Common::session('envelope_order_id',null);

            $message = array('url' => Cookie::get('referer', $this->cmsurl), 'status' => 1);
            $ucsynlogin = '';
            if (defined('UC_API') && @include_once BASEPATH . '/uc_client/client.php')
            {
                $uid = uc_user_register($user, $pwd, $user);
                if ($uid > 0)
                {
                    $ucsynlogin = uc_user_synlogin($uid);
                }
            }
            $message['js'] = $ucsynlogin;
            Plugin_Core_Factory::factory()->add_listener('on_member_register', ORM::factory('member', $insertId)->as_array())->execute();

            $member = DB::select()->from('member')->where('mid','=',$insertId)->execute()->current();
            Model_Member_Grade::new_user_gift($member);

            exit(json_encode($message));
        } else
        {
            $message = array('msg' => __("error_member_insert"), 'status' => 0);
            exit(json_encode($message));
        }
    }

    /**
     * ajax 发送短信验证码
     */
    public function action_ajax_send_message()
    {
        $frmcode = Common::remove_xss(Arr::get($_POST, 'frmcode'));
        $code = Common::remove_xss(Arr::get($_POST, 'code'));

        if ($frmcode != Common::session('crsf_code'))
        {
            Common::session('captcha_response', null);
            exit(__('error_frmcode'));
        }
        //验证码验证
        if (!Captcha::valid($code) || empty($code))
        {
            Common::session('captcha_response', null);
            exit(__('account_code_error'));
        }

        $validataion = Validation::factory($this->request->post());
        $validataion->rule('phone', 'not_empty');
        $validataion->rule('phone', 'phone');
        if (!$validataion->check())
        {
            exit(__('account_mobile_error'));
        }
        //检测用户是否存在
        $phone = Common::remove_xss(Arr::get($_POST, 'phone'));
        $member = Model_Member::member_find($phone);
//        if (!empty($member))
//        {
//            Common::session('captcha_response', null);
//            exit(__('error_user_exists'));
//        }
        $code = rand(1000, 9999);

        Common::session($phone.'_msg_code',null);
        $status = json_decode(St_SMSService::send_member_msg($phone,NoticeCommon::MEMBER_REG_CODE_MSGTAG,"","",$code));
        if($status->Success){
            Common::session($phone.'_msg_code', $code);
        }
        echo intval($status->Success);
    }
    /**
     * ajax 发送邮件验证码
     */
    public function action_ajax_send_email_message()
    {
        $frmcode =$_POST['frmcode'];
        $code = $_POST['code'];

        if ($frmcode != Common::session('crsf_code'))
        {
            Common::session('captcha_response', null);
            exit(__('error_frmcode'));
        }
        if (!Captcha::valid($code))
        {
            Common::session('captcha_response', null);
            exit(__('account_code_error'));
        }

        $validataion = Validation::factory($this->request->post());
        $validataion->rule('email', 'not_empty');
        $validataion->rule('email', 'email');
        if (!$validataion->check())
        {
            exit(__('account_email_error'));
        }
        //检测用户是否存在
        $email =$_POST['email'];
        $member = Model_Member::member_find($email);
//        if (!empty($member))
//        {
//            Common::session('captcha_response', null);
//            exit(__('error_user_exists'));
//        }
        $code = rand(1000, 9999);
        Common::session($email.'_msg_code', null);
        $result = St_EmailService::send_member_email($email,NoticeCommon::MEMBER_REG_CODE_MSGTAG,'',$code);
        if($result)
        {
            Common::session($email.'_msg_code', $code);
        }
        echo intval($result);
    }

}